<?php
	global $global_ws_address;

	$context = stream_context_create(array(
		'http' => array(
			'timeout' => 10      // Timeout in seconds
		)
	));

    $url1 = $global_ws_address.'o=allproduct';
	$result1 = @file_get_contents($url1,0,$context);
	$productList = json_decode($result1, true);

	$r = $_POST['chooseProduct'];
	if( $r != null && $r != '' ){
		$url = $global_ws_address.'o=rankplist&p1='.$r.';&p2=0';
		$result = @file_get_contents($url,0,$context);
		$result = json_decode($result, true);
	}
?>

<html>
<head>
	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript">
	    $(document).ready(function(){
			<?php
				if( $r ){
					echo "$('#top10_result').show();";
				}
				else{
					echo "$('#top10_result').hide();";
				}
			?>
		});
	</script>
	
	<style type="text/css"> 
		#top10_input{
			margin:5px;
			padding:5px;
			background:#e5eecc;
			border:solid 2px #c3c3c3;
		}
                
       #top10_result{
            margin:5px;
			padding:5px;
			background:#F5F5DC;
			border:solid 2px #c3c3c3;
        }	
	</style>
</head>
 
<body>

	<p>
		Choose a product name in the drop-down list below to see its TOP10 CVEs.
	</p>

	<div id="top10_input">
		<fieldset>
			  <legend>Product Name</legend>
			  <form method="post" action="">
					<select name="chooseProduct">
						<option value="">Products.</option>
						<?php
							if( is_array($productList)){
								foreach( $productList as $index => $item ){
									echo '<option value="'.$item['product'].'">'.$item['product'].'</option>';
								}
							}
						?>
					</select>
					<input type="submit" name="submit" value="Top10!"/>
			  </form>
		</fieldset>
	</div>

	<div id="top10_result" style="text-align:center;display:none">
		Top10 CVEs for <?php echo $r?><br/>
		<?php
			if( is_array($result)){
				
				$chart = array(
				  '#chart_id' => 'result',
				  '#title' => chart_title(t('Rank Result'), '0000ee', 15),
				  '#type' => CHART_TYPE_BAR_V,
				  '#size' => chart_size(700, 200),
				  '#bar_size' => chart_bar_size(40,10),
				);

				$i = 0;
				$x_axis1 = '';
				$x_axis2 = '|';
			  
				foreach( $result as $index => $value ){
				  $chart['#data'][$value['cvename']] = t($value['total']);
				  if( $i % 2 == 0 )  $x_axis1 .= $value['cvename'].'||';
				  else  $x_axis2 .= $value['cvename'].'||';
				  $i++;
				}

				$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][0][] = chart_mixed_axis_label(t($x_axis1));
				if( $i > 1 )
				  $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t($x_axis2));

				$max = ceil($result[0]['total']/50) * 50;

				$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
				$chart['#data_scale'] = array('#min' => 0, '#max' => $max);

				$chart['#shape_markers'][] = array('N','000000',0,-1,11);

				echo chart_render($chart);

				echo '<br/>';
				echo '<ul style="text-align:left">';
				echo 'Click to see details.';
				foreach($result as $index => $value){
					echo '<li><a href="?q=node/14&cve='.$value['cvename'].'">'.$value['cvename'].'</a></li>';
				}
				echo '</ul>';

			}else{
				echo 'No results.';
			}
		?>
	</div>
</body>
</html>

